package com.link.base.base.wechat.controller;

import com.link.base.base.wechat.model.Menu;
import com.link.base.base.wechat.service.MenuService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * 微信公众号自定义菜单控制器
 *
 * @author lujiang
 * @date 2019/02/26 10:00
 */
@Controller
@RequestMapping("/link/wxmenucontroller")
public class WxMenuController extends BasicController<Menu> {
    @Resource
    private MenuService menuService;

    @Override
    public BasicService<Menu> getBasicService() throws Exception {
        return menuService;
    }

    /**
     * 创建微信公众号自定义菜单
     *
     * @author lujiang
     * @date 2019/02/26
     * @param menulist  菜单集合
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/createWechatMenu", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> createWxMenu(@RequestBody List<Menu> menulist) throws Exception {
        Map<String, Object> result = new HashMap<>(16);
        try {
            result = menuService.createWxMenu(menulist);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 清空自定义菜单
     *
     * @author lujiang
     * @date 2019/02/26 9:08
     * @param wx_id 微信公众号记录id
     * @return
     */
    @RequestMapping(value = "/deleteWxMenu", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> deleteWxMenu(Long wx_id) {
        Map<String, Object> map = new HashMap<>(16);
        try {
            map = menuService.deleteWxMenu(wx_id);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg",e.getMessage());
            LCLogger.withException(e);
        }

        return map;
    }

    /**
     * 获取自定义菜单配置接口
     *
     * @author lujiang
     * @date 2019/02/26 9:27
     * @param wx_id 微信公众号row_id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryWxMenu", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryWxMenu(String wx_id) throws Exception {
        Map<String, Object> map = new HashMap<>(16);
        try {
            map = menuService.queryWxMenu(wx_id);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg",e.getMessage());
            LCLogger.withException(e);
        }
        return map;

    }

    /**
     * 删除,修改，新增二级菜单
     *
     * @author lujiang
     * @date 2019/02/26 9:27
     * @param menulist 微信公众号自定义菜单集合
     * @return
     */
    @RequestMapping(value = "/updateWxMenu", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> deleteSecondWxMenu(@RequestBody List<Menu> menulist) {
        Map<String, Object> map = new HashMap<>(16);
        try {
            map = menuService.deleteSecondWxMenu(menulist);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg",e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * 删除菜单
     *
     * @author lujiang
     * @date 2019/02/26 9:27
     * @param ids row_id集合
     * @return
     */
    @RequestMapping(value = "/deleteMenu", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteMenu(Long[] ids) {
        Map<String, Object> map = new HashMap<>(8);

        try {
            map = menuService.deleteMenu(ids);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            LCLogger.withException(e);
            map.put("msg",e.getMessage());
        }
        return map;
    }

}
